package org.cuckoos.android.ui;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.cuckoos.android.ContactBook;
import org.cuckoos.android.R;
import org.cuckoos.android.SocketService;
import org.cuckoos.android.command.EntryCommand;
import org.cuckoos.android.domain.Command;
import org.cuckoos.android.domain.Constant;
import org.cuckoos.android.domain.User;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AppActivity extends Activity implements ChatPanel{
//	private List<User> contactBook = new ArrayList<User>();
	ContactBook book ;
	private ListView contactBookView;
	private SocketService s;
	private boolean isStop;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		s = SocketService.getDefault();
		s.start(this);
		initContactBook();
		overlordEntry();
	}
	
	

	@Override
	protected void onDestroy() {
		s.stop();
		super.onDestroy();
	}



	private void overlordEntry() {
		User user = null;
		try {
			user = new User(Constant.DEFAULT_NAME, InetAddress.getLocalHost().getHostName());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		EntryCommand ec = new EntryCommand(s.getSocket(), user);
		ec.execute();
//		book.updateAll();
	}

	private void initContactBook() {
		book = new ContactBook(this);
		contactBookView = (ListView) this.findViewById(R.id.ContactBook);
		contactBookView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				User user = (User) parent.getItemAtPosition(position);
				Intent input = new Intent(AppActivity.this, InputActivity.class);
				input.putExtra(Constant.DEFAULT_USER_KEY, user);
				startActivity(input);
			}
		});
	}

	/**
	 * 更新用户列表,必须在UIThread中执行
	 */
	public void updateUserList(User user) {
		Log.d(this.getClass().getSimpleName(), user.getName());
		book.addUser(user);
	}

	/**
	 * 处理接受到的消息,必须在UIThread中执行
	 * 
	 * @param msg
	 */
	public void showMessage(final Command msg) {
		this.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				if(isStop){
					return;
				}
				new AlertDialog.Builder(AppActivity.this)
						.setTitle("来自" + msg.getUser().getName())
						.setMessage(msg.getMsg())
						.setPositiveButton(R.string.reciveMsg, new OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								Intent input = new Intent(AppActivity.this, InputActivity.class);
								input.putExtra(Constant.DEFAULT_USER_KEY, msg.getUser());
								startActivity(input);
							}
						})
						.show();

			}
		});
	}

	public void reciveFile(File f) {

	}



	public ListView getContactBookView() {
		return contactBookView;
	}



	@Override
	protected void onStop() {
		super.onStop();
		isStop = true;
	}



	@Override
	public void sendMessage() {
		
	}
	
	
    



}